package cn.itcast.nems.listener.studentrecord;

import cn.hutool.core.lang.Assert;
import cn.itcast.nems.common.tenant.TenantInfo;
import cn.itcast.nems.common.tenant.TenantInfoHolder;
import cn.itcast.nems.listener.NemsMessageListener;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordMessageDTO;
import cn.itcast.nems.studentrecord.service.ClazzEnrollmentCountService;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
/**
 * <p>
 * 学籍创建消息监听处理逻辑
 * </p>
 *
 * @author zhouwei
 * @since 2023/12/7 2:07 PM
 **/
@Component
@RequiredArgsConstructor
public class StudentRecordCreatedMessageListener implements NemsMessageListener {
    private final static String TagName  =  "TAG_STUDENT_RECORD_CREATED" ;
    private final ClazzEnrollmentCountService clazzEnrollmentCountService ;

    @Override
    public String getTagName() {
        return TagName;
    }

    @Override
    public synchronized void processMessage(String body) {
        // 监听学籍创建消息
        StudentRecordMessageDTO studentRecordMessageDTO = JSON.parseObject(body, StudentRecordMessageDTO.class);
        Assert.notNull(studentRecordMessageDTO, "【消息监听】tag: TAG_STUDENT_RECORD_CREATED,消息体中业务数据实体为空");
        Assert.notBlank(studentRecordMessageDTO.getStudentRecordId(), "【消息监听】tag: TAG_STUDENT_RECORD_CREATED,学籍ID为空");
        TenantInfoHolder.setTenantInfo(new TenantInfo(studentRecordMessageDTO.getTenantId(), studentRecordMessageDTO.getSubtenantId()));
        clazzEnrollmentCountService.calculatePaidCount(studentRecordMessageDTO.getStudentRecordId());
    }
}
